﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sendz.Domain;
using Sendz.WebUI.Models;
using System.Web.Mvc;
using Sendz.Model;
using Sendz.WebUI.Infrastructure;
using Ninject;

namespace Sendz.WebUI.Helpers
{

    public class FormDataHelpers
    {
        // Build a SelectList enumerable collection for all autoresponders
        public List<SelectListItem> GetAutoResponders(Form thisForm, Guid userKey)
        {
            // Todo: [MOB HELP!] make this IoC instead of literal EF call--ModelBinders are a pain for this and needing parameterless constructors
            // Also, other IoC patterns seem to fail here (ref: http://ninject.codeplex.com/wikipage?title=Injection%20Patterns)
            var dbAutoResponder = new EntityAutoResponderRepository();
            var autoResponders = dbAutoResponder.GetAutoRespondersByUserKey(userKey);
            if (autoResponders.Count > 0)
            {
                var autoResponderList = new List<SelectListItem>();
                autoResponderList.Add(new SelectListItem { Text = "--None, unassigned--", Value = string.Empty });
                var selected = false;
                foreach (var autoResponder in autoResponders)
                {
                    if (thisForm != null)
                    {
                        if (thisForm.AutoResponderKey == autoResponder.AutoResponderKey)
                            selected = true;
                        else
                            selected = false;
                    }
                    else
                        selected = false;
                    autoResponderList.Add(new SelectListItem { Text = autoResponder.Name, Value = autoResponder.AutoResponderKey.ToString(), Selected = selected });
                }
                return autoResponderList;
            }
            return null;
        }

        // Build a SelectList enumerable collection for the provided field
        public List<SelectListItem> GetFieldOptions(FormField formField, FormFieldViewModel formresponse = null)
        {
            var optionsList = new List<SelectListItem>();
            // Build the optionsList for choice-based form fields
            if (formField.ListEnum.IndexOf("choice") > -1)
            {
                // Add a blank choice for this field type
                if (formField.ListEnum == "field_choice_selectlist")
                    optionsList.Add(new SelectListItem { Text = "--Select an option--", Value = string.Empty });
                var splitOptions = formField.Options.Split('\n');
                // Kill any other newline references
                for (var i = 0; i < splitOptions.Count(); i++)
                    splitOptions[i] = splitOptions[i].Replace("\r", string.Empty);
                var selected = false;
                foreach (var option in splitOptions)
                {
                    if (formresponse != null)
                    {
                        if (formresponse.Value != null)
                        {
                            if (formresponse.Value.FirstOrDefault(fr => fr == option) != null)
                                selected = true;
                            else
                                selected = false;
                        }
                    }
                    // Commas would impact the Request.Form and split the values too much so this serves as a manual delimiter
                    optionsList.Add(new SelectListItem { Text = option, Value = option.Replace(",", "%comma%"), Selected = selected });
                }
            }
            return optionsList;
        }
    }
}